// pages/index/index.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //学生的基本信息
    userInfo: [],
    // 展开折叠
    selectedFlag: [false],
    examineSchool: [],//本科院校
    examineMajor: [],//本科专业
    schoolId: null, //学校id
    marjorId: null, //专业id
    schoolIndex: null, //选中学校索引
    marjorIndex: null, //选中专业索引
    parentPhoneNumber: null, //家长电话
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfo(); //基本信息
    this.getExamineSchool(); //本科院校
  },
  //获取学生基本信息
  getUserInfo() {
    var token = wx.getStorageSync('token');
    // console.log('获取app的token' + token);
    let that = this;
    wx.request({
      url: app.api.getUserInfo,
      data: {
        type: 'space-style-section',
      },
      header: {
        'Content-Type': 'application/json',
        'Authorization': token
      },
      success(res) {
        // console.log("res", res.data);
        if (res.data.code === 0) {
          that.setData({
            userInfo: res.data.data,
            parentPhoneNumber: res.data.data.parentPhoneNumber
          })
        } else if (res.data.code === 100) {
          wx.reLaunch({
            url: '/pages/login/login'
          })
        }
      }
    })
  },
  //获取本科院校信息
  getExamineSchool() {
    var token = wx.getStorageSync('token');
    let that = this;
    wx.request({
      url: app.api.getExamineSchool,
      data: {
        type: 'space-style-section'
      },
      header: {
        'Content-Type': 'application/json',
        'Authorization': token
      },
      success(res) {
        // console.log("examineSchool", res.data.data);
        if (res.data.code === 0) {
          that.setData({
            examineSchool: res.data.data
          })
        } else if (res.data.code === 100) {
          wx.reLaunch({
            url: '/pages/login/login'
          })
        }
      }
    })
  },
  // 获取学校id
  bindSchoolChange(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    // console.log("##  " + this.data.examineSchool[e.detail.value].id)
    this.setData({
      schoolIndex: e.detail.value,
      schoolId: this.data.examineSchool[e.detail.value].id,
    })
    this.getMajor()
  },
  // 获取专业id
  bindMajorChange(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    // console.log('发送选择改变携带值为', e)
    // console.log("##  " + this.data.examineMajor[e.detail.value].id)
    this.setData({
      marjorIndex: e.detail.value,
      marjorId: this.data.examineMajor[e.detail.value].id,
    })
  },
  //获取本科专业
  getMajor() {
    var token = wx.getStorageSync('token');
    let that = this;
    console.log("564", that.data.schoolId);

    wx.request({
      url: app.api.getMajor,
      data: {
        examineSchoolId: that.data.schoolId,
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': token
      },
      success(res) {
        console.log("examineMajor", res.data.data);
        if (res.data.code === 0) {
          that.setData({
            examineMajor: res.data.data
          })
        }
      }
    })
  },
  // 获取家长电话号
  parentPhoneNumberChange(e) {
      // console.log("jiaz",e.detail.value);
    this.setData({
      parentPhoneNumber: e.detail.value,
    })
    
  },
  // 提交报名表单
  enrollSubmit(e) {
    // console.log("id", that.data.userInfo.id);
    const params = this.data.parentPhoneNumber
    console.log("ccc", params);
    var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    //校验表单
    if (params == '') {
      //报错 
      wx.showModal({
        content: '请输入家长手机号码',
        showCancel: false,
      })
      return false
    } else if (!myreg.test(params)) {

      wx.showModal({
        content: '请输入正确的家长手机号码',
        showCancel: false,
      })
      return false;

    } else {
      let that = this
      wx.login({
        success: function (res) {
          // console.log('获取code66', res.code);
          wx.setStorageSync('wxCode', res.code)
          if (res.code) {
            // console.log("9090", wx.getStorageSync('token'));
            wx.request({
              url: app.api.submit + '?code=' + wx.getStorageSync('wxCode'),
              header: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Authorization": wx.getStorageSync('token'),
              },
              method: "POST",
              data: {
                'examinationGradeId': that.data.userInfo.examinationGradeId,
                'examineMajorId': that.data.marjorId,
                'examineSchoolId': that.data.schoolId,
                'id': that.data.userInfo.id,
                'parentPhoneNumber': that.data.parentPhoneNumber,
                'shortPhoneNumber': '',
                'userId': that.data.userInfo.userId,
              },
              success: function (res) {
                if (res.data.code == 0) {
                  wx.showToast({
                    title: '提交成功',
                    // icon: 'loading',
                    // duration: 1500
                  })
                } else {
                  wx.showToast({
                    title: res.data.message,
                    // icon: 'loading',
                    // duration: 1500
                  })
                }

              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      });
    }
  },
  // 展开折叠选择  
  changeToggle: function (e) {
    var index = e.currentTarget.dataset.index;
    if (this.data.selectedFlag[index]) {
      this.data.selectedFlag[index] = false;
    } else {
      this.data.selectedFlag[index] = true;
    }

    this.setData({
      selectedFlag: this.data.selectedFlag
    })
  },
})